<?php

class GoodsController extends Admin {
    
    private $table;
	private $verify;
    
    public function __construct() {
		parent::__construct();
        $this->tree = $this->instance('tree');
        $this->tree->config(array('id' => 'catid', 'parent_id' => 'parentid', 'name' => 'catname'));
	}

    public function addAction()
    {
//        echo "hello I am vic! ";
        /*$model = $this->model('goods');
        $data = $model->getAll('id=?', "1");
        $this->view->assign('data',$data); //assign()对视图变量完成赋值
        $this->view->display('admin/goods/goods'); //加载视图文件*/

        $model = $this->get_model();
        $fields = $model[1]['fields'];
        $data_fields = $this->getFields($fields, array());

        $goodsModel = $this->model('goods');
//        $data = $goodsModel->getOne('id=?', "1");

        if ($this->post('submit')) {    //表单提交
            $data = $this->post('data');
            $time = time();
            $insert_array = array(
                'name'=>$data['name'],
                'price'=>$data['price'],
                'category'=>$data['category'],
                'keywords'=>$data['keywords'],
                'description'=>$data['description'],
                'content'=>$data['content'],
                'hot'=>$data['hot'],
                'stock'=>$data['stock'],
                'buy'=>$data['buy'],
                'source'=>$data['source'],
                'status'=>$data['status'],
                'create_time'=>$time,
                'update_time'=>$time,
            );
            $result = $goodsModel->insert($insert_array);   //商品入库
            if (!is_numeric($result)) {
                $this->adminMsg($result);
            }
            $data['id'] = $result;

            $imagesModel = $this->model('images');
            $images_array = array(
                'type'=>$data['id'],
                'src'=>$data['thumb'],
                'create_time'=>$time,
                'update_time'=>$time,
            );
            $resultImages = $imagesModel->insert($images_array);   //商品banner图片入库
            if (!is_numeric($resultImages)) {
                $this->adminMsg($resultImages);
            }

            $this->postEvent($data, 'later', 'admin');	//发布后事件
            if ($this->site['SITE_MAP_AUTO'] == true) {
                $this->sitemap();
            }
            $this->toHtml($data);
            $msg = '<a href="' . url('admin/goods/add') . '" style="font-size:14px;">' . lang('a-con-7') . '</a>&nbsp;&nbsp;<a href="' . url('admin/content/index', array('catid' => $data['catid'], 'modelid' => $modelid)) . '" style="font-size:14px;">' . lang('a-con-8') . '</a>';
            $this->adminMsg(lang('a-con-9') . '<div style="padding-top:10px;">' . $msg . '</div>', '', 3, 0, 1);
        }

        $categoryModel = $this->model('goods_category');
        $categoryModel->from(array('goods_category'), array('name','id'));
        $categoryModel->where('pid=?', '1');
        $category = $categoryModel->select();   //获取所有商品类别

        $this->view->assign(array(
            'data'=>$data,
            'category'=>$category,
            'data_fields' => $data_fields
        )); //assign()对视图变量完成赋值
        $this->view->display('admin/goods/goods_add'); //加载视图文件
    }

    public function listAction()
    {

    }

    /**
     * 生成/删除内容页HTML文件
     */
    private function toHtml($data) {
        if (is_array($data) && isset($data['id'])) $this->createShow($data);
        if (is_numeric($data)) {
            $data = $this->content->find((int)$data);
            $this->createShow($data);
        }
    }
	

}